import '@babel/polyfill';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import gsap from 'gsap';
import * as dat from 'dat.gui';
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer';
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass';
import { DotScreenPass } from 'three/examples/jsm/postprocessing/DotScreenPass';
import { SMAAPass } from 'three/examples/jsm/postprocessing/SMAAPass';
import { GlitchPass } from 'three/examples/jsm/postprocessing/GlitchPass';
import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass';
import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass';

//场景
const scene = new THREE.Scene();

//相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(5, 3, 5);
scene.add(camera);

//渲染器
const renderer = new THREE.WebGL1Renderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//后处理渲染器
const effectComposer = new EffectComposer(renderer);
effectComposer.setSize(window.innerWidth, window.innerHeight);

//渲染通道
const renderPass = new RenderPass(scene, camera);
effectComposer.addPass(renderPass);

//像素点效果
//const dotScreenPass = new DotScreenPass();
//effectComposer.addPass(dotScreenPass);

//抗锯齿效果
const sMAAPass = new SMAAPass();
effectComposer.addPass(sMAAPass);

//屏幕闪动效果
//const glitchPass = new GlitchPass();
//effectComposer.addPass(glitchPass);

//物体发光效果
const unrealBloomPass = new UnrealBloomPass();
effectComposer.addPass(unrealBloomPass);
unrealBloomPass.exposure = 0;
unrealBloomPass.strength = 2;
unrealBloomPass.radius = 1;
unrealBloomPass.threshold = 0;//阈值

//改写着色器 效果  【作用于整个场景】
const shaderPass = new ShaderPass({
    vertexShader:
        `
        varying vec2 vUv;
        void main(){
            vUv=uv;
            gl_Position=projectionMatrix*viewMatrix*modelMatrix*vec4(position,1.0);
        }
    `,
    fragmentShader:
        `
        varying vec2 vUv;
        //tDiffuse:当场景的纹理,通过uv进行采样
        uniform sampler2D tDiffuse;
        uniform vec3 uColor;
        void main(){
            vec4 color=texture2D(tDiffuse,vUv);
            color.rgb+=uColor;
            gl_FragColor=color;
        }
    
    `,
    uniforms: {
        uColor: {
            value: new THREE.Color(0, 0.3, 0.5)
        }
    }
});
effectComposer.addPass(shaderPass);


//平行光
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(0, 0, 100);
scene.add(light);


//创建背景板
const plane = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), new THREE.MeshStandardMaterial({ color: 0xff0000 }));
plane.position.set(0, 0, 0);
scene.add(plane);

//控制
const orbitControls = new OrbitControls(camera, renderer.domElement);
orbitControls.enableDamping = true;


//坐标轴
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

//帧动画
function animate() {
    orbitControls.update();
    //renderer.render(scene, camera);
    effectComposer.render();
    requestAnimationFrame(animate);
};
animate();

//窗口调整侦听
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
});



